import Cookies from "js-cookie"

const STATUS = getSidebarStatus()

const state = {
  sidebar: {
    open: STATUS ? !!+STATUS : true,
    withoutAnimation: false
  },
  device: 'desktop'
}

const mutations = {
  CLOSE_SIDEBAR: (state, withoutAnimation) => {
    state.sidebar.open = false
    state.sidebar.withoutAnimation = withoutAnimation
    setSidebarStatus()
  },
  TOGGLE_SIDEBAR: (state) => {
    state.sidebar.open = !state.sidebar.open
    state.sidebar.withoutAnimation = false
    setSidebarStatus()
  },
  TOGGLE_DEVICE: (state, device) => {
    state.device = device
  }
}

const actions = {
  closeSideBar({commit}, { withoutAnimation }) {
    commit('CLOSE_SIDEBAR', withoutAnimation)
  },
  toggleSideBar({commit}) {
    commit('TOGGLE_SIDEBAR')
  },
  toggleDevice({commit}, device) {
    commit('TOGGLE_DEVICE', device)
  },
}

// 获取侧边栏状态
function getSidebarStatus() {
  return Cookies.get('sidebar-status')
}

// 设置侧边栏状态
function setSidebarStatus() {
  Cookies.set('sidebar-status', +state.sidebar.open)
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}